package com.originskyseed.platform.pk.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.pk.mapper.PkRuleMapper;
import com.originskyseed.platform.pk.model.PkRule;
import com.originskyseed.platform.pk.service.IPkRuleService;

/**
 * pk规则表Service接口实现类（OSI_PK_RULE）
 *
 * @author WuZhuoXi
 */
@DubboService
@Component("pkRuleService")
public class PkRuleServiceImpl implements IPkRuleService {

	@Resource
	private PkRuleMapper pkRuleMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.pkRuleMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.pkRuleMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 批量删除记录数据
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByIds(List<String> ids) throws Exception {
		return this.pkRuleMapper.deleteByIds(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PkRule record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.pkRuleMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PkRule record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.pkRuleMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PkRule record) throws Exception {
		record.setUpdateTime(new Date());
		return this.pkRuleMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PkRule record) throws Exception {
		record.setUpdateTime(new Date());
		return this.pkRuleMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PkRule selectByPrimaryKey(String id) throws Exception {
		return this.pkRuleMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PkRule> selectList(PkRule record) throws Exception {
		return this.pkRuleMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PkRule> selectListByPage(Map<String, Object> params, BasePage<PkRule> page) throws Exception {
		params.put("page", page);
		List<PkRule> list = this.pkRuleMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 发起PK页面规则列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PkRule> selectPkRuleListByPage(Map<String, Object> params, BasePage<PkRule> page) throws Exception {
		params.put("page", page);
		List<PkRule> list = this.pkRuleMapper.selectPkRuleListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量禁用启用
	 *
	 * @param ids
	 * @param state
	 * @param updateTime
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateBatchStateByIds(List<String> ids, Integer state, Date updateTime) throws Exception {
		return this.pkRuleMapper.updateBatchStateByIds(ids, state, updateTime);
	}

}
